#include "air720.h"
#include "usart.h"
#include "mqtt.h" //包含需要的头文件
#include "FreeRTOS.h"
#include "task.h"
//#include "utils_aes.h"	//包含需要的头文件
//#include "utils_sha256.h" //包含需要的头文件
//#include "system.h"

__align(8) char Temp_TxBuff[256];
void AIR720_printf(char *fmt, ...)
{
//	taskENTER_CRITICAL();
	int i, length;
	memset(Temp_TxBuff, 0, sizeof(Temp_TxBuff));

	va_list ap;
	va_start(ap, fmt);
	vsprintf(Temp_TxBuff, fmt, ap);
	va_end(ap);

	length = strlen((const char *)Temp_TxBuff);
	while ((AIR7200_USE_USART->SR & 0X40) == 0)
		;
	for (i = 0; i < length; i++)
	{
		AIR7200_USE_USART->DR = Temp_TxBuff[i];
		while ((AIR7200_USE_USART->SR & 0X40) == 0)
			;
	}
//	taskEXIT_CRITICAL();
}

//查找字符串，存在 return 0，不存在 return 1
u8 bufstr(u8 * buf,u16 buflen,char * str)
{
  if(buflen < strlen(str))
		return 1;
	for(int i=0;i<=buflen-strlen(str);i++)
	{
		if(memcmp(&buf[i],str,strlen(str)) == 0)
			return 0;
	}

	return 1;
}


/*
UART Boot Completed
RDY
89860319747680994940
^SIMST: 0
+CPIN: READY
^SIMST: 1
+CGEV: EPS PDN ACT 5
+NITZ: 19/09/23,13:24:41+32,0
SMS READY
*/
u8 AIR_WaitReday()
{
	printf("等待模块正常 \r\n");
	int timeout = 100;
	AIR720_RxCounter = 0;						  //7020接收数据量变量清零
	memset(AIR720_RX_BUF, 0, AIR720_RXBUFF_SIZE); //清空7020接收缓冲区
	while (timeout--)
	{												  //等待超时时间到0
		printf("%d ", timeout);						//串口输出现在的剩余超时时间
		delay_ms(1000);									//延时100ms
//			if (strstr((char *)AIR720_RX_BUF, "SMS READY")) //如果接收到OK表示指令成功
		if (bufstr(AIR720_RX_BUF,AIR720_RxCounter,"SMS READY") == 0) //如果接收到OK表示指令成功
			break;										//主动跳出while循环
	}
	if (timeout <= 0)
	{
		//@ 开机失败，等待开机数据超时
		printf("开机失败 \r\n");
		return 1;
	}
	return 0;
}

u8 AIR720_COMM_Cmd(int loopNum, char *cmd_str, char *verify_str, u16 loop_delay)
{
	while (loopNum--)
	{
		AIR720_RxCounter = 0;						  						//7020接收数据量变量清零
		memset(AIR720_RX_BUF, 0, AIR720_RXBUFF_SIZE); //清空7020接收缓冲区
		AIR720_printf(cmd_str); //发送指令
		printf("%d ", loopNum); //串口输出现在的剩余超时时间
		delay_ms(loop_delay);	  //延时
		if (strstr((char *)AIR720_RX_BUF, verify_str))
			break;
	}

	if (loopNum < 0)
	{
		printf("\r\n命令执行失败 \r\n");
		//@ 开机失败，等待开机数据超时
		return 1;
	}
	else
	{
		printf("\r\n命令执行成功 \r\n");
	}
	//@ 执行命令成功
	return 0;
}

u8 AIR720_ConnecteService(char * ip,u16 port,int loopNum,u16 loop_delay)
{
	char cmd_str[128],isok = 0;
	sprintf(cmd_str,"AT+CIPSTART=\"TCP\",\"%s\",\"%u\"\r\n",ip,port);

//	isok = AIR720_COMM_Cmd(5,cmd_str, "CONNECT", 5000);
	
	while(loopNum--)
	{
		memset(AIR720_RX_BUF, 0, AIR720_RXBUFF_SIZE); //清空7020接收缓冲区
		u2Receiver.receivedLen = 0;
		AIR720_printf(cmd_str); //发送指令
		printf("%d ", loopNum); //串口输出现在的剩余超时时间
		delay_ms(loop_delay);
		
		char * ret1 = strstr((char *)AIR720_RX_BUF,"ALREADY CONNECT");
		char * ret2 = strstr((char *)AIR720_RX_BUF,"CONNECT FAIL");
		char * ret3 = strstr((char *)AIR720_RX_BUF,"CONNECT");
		if(ret1 || ret2)
		{
			continue;
		}else if(ret3)
		{
			printf("\r\n命令执行成功 \r\n");
			return 0;
		}
	}
	
	if(loopNum < 0)
	{
		printf("\r\n命令执行超时 \r\n");
		return 1;
	}else 
	{
		printf("\r\n命令执行成功\r\n");
	}
	
	return 0;
}
